package dao

import (
	"errors"
	"gorm.io/gorm"
	"yun_image/config/image"
	"yun_image/entity"
)

func CreateUserImg(img *entity.UserImg) error {
	result := DB.Create(img)
	return result.Error
}
func DeleteUserImgs(ids []int, account string) error {
	result := DB.Model(&entity.UserImg{}).Where("id in ? and account = ?", ids, account).Update("is_deleted", image.Deleted)
	return result.Error
}
func CreateYunImg(img *entity.YunImg) error {
	result := DB.Create(img)
	return result.Error
}

func GetUserImgsByAccount(imgs *[]entity.UserImg, account string) error {
	result := DB.Where("account = ? and is_deleted = ?", account, image.NotDeleted).Find(imgs)
	return result.Error
}

func GetUserImgsByCategoryId(imgs *[]entity.UserImg, categoryId int) error {
	result := DB.Where("category_id = ? and is_deleted = ?", categoryId, image.NotDeleted).Find(imgs)
	return result.Error
}

func GetYunImgById(img *entity.YunImg, id string) error {
	result := DB.Where("id = ?", id).Take(img)
	if errors.Is(result.Error, gorm.ErrRecordNotFound) || result.Error == nil {
		return nil
	}
	return result.Error
}
func UpdateUserImgCategoryId(userImgId int, categoryId int, account string) error {
	result := DB.Model(&entity.UserImg{}).Where("id = ? and account = ?", userImgId, account).
		Update("category_id", categoryId)
	return result.Error
}
